class Homework {
  constructor() {
    this.dbName = 'snd_ai_homework'
    this.dbVersion = 10
    this.db = undefined // 数据库对象
  }

  init(studentId) {
    // 打开一个数据库并获取其reques对象
    const request = indexedDB.open(`${this.dbName}_user_${studentId}`, this.dbVersion)

    // 数据库打开失败
    request.onerror = (event) => {
      console.error('indexDB: 打开数据库失败！', event.target.error)
    }

    // 数据库打开成功
    request.onsuccess = (event) => {
      const db = event.target.result
      // console.log('indexDB: 打开数据库成功', db)
      this.db = db
    }

    // 数据库版本变更时候会触发
    request.onupgradeneeded = (event) => {
      // 数据库对象
      const db = event.target.result
      console.warn('indexDB: 数据库版本变更', db.version)

      if (db.objectStoreNames.contains('cache')) {
        db.deleteObjectStore('cache')
      }

      // 创建一个数据表
      db.createObjectStore('cache', {
        keyPath: 'id'
      })

      if (!db.objectStoreNames.contains('exam_ids')) {
        // 创建一个数据表
        db.createObjectStore('exam_ids', {
          keyPath: 'subjectId'
        })
      }

      this.db = db
    }
  }

  setCacheItem(data) {
    const transaction = this.db.transaction(['cache'], 'readwrite')
    const objectStore = transaction.objectStore('cache')
    const request = objectStore.add(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('添加成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('添加失败', event.target.error)
    }
  }

  getCacheItem(id) {
    return new Promise((resolve) => {
      const transaction = this.db.transaction(['cache'], 'readwrite')
      const objectStore = transaction.objectStore('cache')
      const request = objectStore.get(id)
      // 成功
      request.onsuccess = (event) => {
        if (event.target.result) {
          const { value } = event.target.result
          resolve({
            success: true,
            data: value
          })
        } else {
          resolve({
            success: false,
            data: null
          })
        }
      }
    })
  }

  putCacheItem(data) {
    const transaction = this.db.transaction(['cache'], 'readwrite')
    const objectStore = transaction.objectStore('cache')
    const request = objectStore.put(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('更新成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('更新失败', event.target.error)
    }
  }

  deleteCacheItem(id) {
    const transaction = this.db.transaction(['cache'], 'readwrite')
    const objectStore = transaction.objectStore('cache')
    const request = objectStore.delete(id)

    request.onsuccess = (event) => {
      if (event.type == 'success') {
        // 成功
      }
    }
  }

  setExamIdsItem(data) {
    const transaction = this.db.transaction(['exam_ids'], 'readwrite')
    const objectStore = transaction.objectStore('exam_ids')
    const request = objectStore.add(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('添加成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('添加失败', event.target.error)
    }
  }

  getExamIdsItem(id) {
    return new Promise((resolve) => {
      const transaction = this.db.transaction(['exam_ids'], 'readwrite')
      const objectStore = transaction.objectStore('exam_ids')
      const request = objectStore.get(id)
      // 成功
      request.onsuccess = (event) => {
        if (event.target.result) {
          const { value } = event.target.result
          resolve({
            success: true,
            data: value
          })
        } else {
          resolve({
            success: false,
            data: null
          })
        }
      }
    })
  }

  putExamIdsItem(data) {
    const transaction = this.db.transaction(['exam_ids'], 'readwrite')
    const objectStore = transaction.objectStore('exam_ids')
    const request = objectStore.put(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('更新成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('更新失败', event.target.error)
    }
  }

  deleteExamIdsItem(id) {
    const transaction = this.db.transaction(['exam_ids'], 'readwrite')
    const objectStore = transaction.objectStore('exam_ids')
    const request = objectStore.delete(id)

    request.onsuccess = (event) => {
      if (event.type == 'success') {
        // 成功
      }
    }
  }
}

class HomeworkInput {
  constructor() {
    this.dbName = 'snd_ai_homework_input'
    this.dbVersion = 8
    this.db = undefined // 数据库对象
  }

  init(studentId) {
    // 打开一个数据库并获取其reques对象
    const request = indexedDB.open(`${this.dbName}_user_${studentId}`, this.dbVersion)

    // 数据库打开失败
    request.onerror = (event) => {
      console.error('indexDB: 打开数据库失败！', event.target.error)
    }

    // 数据库打开成功
    request.onsuccess = (event) => {
      const db = event.target.result
      // console.log('indexDB: 打开数据库成功', db)
      this.db = db
    }

    // 数据库版本变更时候会触发
    request.onupgradeneeded = (event) => {
      // 数据库对象
      const db = event.target.result
      console.warn('indexDB: 数据库版本变更', db.version)

      if (db.objectStoreNames.contains('cache')) {
        db.deleteObjectStore('cache')
      }

      // 创建一个数据表
      db.createObjectStore('cache', {
        keyPath: 'id'
      })

      if (!db.objectStoreNames.contains('exam_ids')) {
        // 创建一个数据表
        db.createObjectStore('exam_ids', {
          keyPath: 'subjectId'
        })
      }

      if (!db.objectStoreNames.contains('subject_settings')) {
        // 创建一个数据表
        db.createObjectStore('subject_settings', {
          keyPath: 'subjectId'
        })
      }

      this.db = db
    }
  }

  setCacheItem(data) {
    const transaction = this.db.transaction(['cache'], 'readwrite')
    const objectStore = transaction.objectStore('cache')
    const request = objectStore.add(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('添加成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('添加失败', event.target.error)
    }
  }

  getCacheItem(id) {
    return new Promise((resolve) => {
      const transaction = this.db.transaction(['cache'], 'readwrite')
      const objectStore = transaction.objectStore('cache')
      const request = objectStore.get(id)
      // 成功
      request.onsuccess = (event) => {
        if (event.target.result) {
          const { value } = event.target.result
          resolve({
            success: true,
            data: value
          })
        } else {
          resolve({
            success: false,
            data: null
          })
        }
      }
    })
  }

  putCacheItem(data) {
    const transaction = this.db.transaction(['cache'], 'readwrite')
    const objectStore = transaction.objectStore('cache')
    const request = objectStore.put(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('更新成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('更新失败', event.target.error)
    }
  }

  deleteCacheItem(id) {
    const transaction = this.db.transaction(['cache'], 'readwrite')
    const objectStore = transaction.objectStore('cache')
    const request = objectStore.delete(id)

    request.onsuccess = (event) => {
      if (event.type == 'success') {
        // 成功
      }
    }
  }

  setExamIdsItem(data) {
    const transaction = this.db.transaction(['exam_ids'], 'readwrite')
    const objectStore = transaction.objectStore('exam_ids')
    const request = objectStore.add(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('添加成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('添加失败', event.target.error)
    }
  }

  getExamIdsItem(id) {
    return new Promise((resolve) => {
      const transaction = this.db.transaction(['exam_ids'], 'readwrite')
      const objectStore = transaction.objectStore('exam_ids')
      const request = objectStore.get(id)
      // 成功
      request.onsuccess = (event) => {
        if (event.target.result) {
          const { value } = event.target.result
          resolve({
            success: true,
            data: value
          })
        } else {
          resolve({
            success: false,
            data: null
          })
        }
      }
    })
  }

  putExamIdsItem(data) {
    const transaction = this.db.transaction(['exam_ids'], 'readwrite')
    const objectStore = transaction.objectStore('exam_ids')
    const request = objectStore.put(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('更新成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('更新失败', event.target.error)
    }
  }

  deleteExamIdsItem(id) {
    const transaction = this.db.transaction(['exam_ids'], 'readwrite')
    const objectStore = transaction.objectStore('exam_ids')
    const request = objectStore.delete(id)

    request.onsuccess = (event) => {
      if (event.type == 'success') {
        // 成功
      }
    }
  }

  setSubjectSettingsItem(data) {
    const transaction = this.db.transaction(['subject_settings'], 'readwrite')
    const objectStore = transaction.objectStore('subject_settings')
    const request = objectStore.add(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('添加成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('添加失败', event.target.error)
    }
  }

  getSubjectSettingsItem(id) {
    return new Promise((resolve) => {
      const transaction = this.db.transaction(['subject_settings'], 'readwrite')
      const objectStore = transaction.objectStore('subject_settings')
      const request = objectStore.get(id)
      // 成功
      request.onsuccess = (event) => {
        if (event.target.result) {
          const { value } = event.target.result
          resolve({
            success: true,
            data: value
          })
        } else {
          resolve({
            success: false,
            data: null
          })
        }
      }
    })
  }

  putSubjectSettingsItem(data) {
    const transaction = this.db.transaction(['subject_settings'], 'readwrite')
    const objectStore = transaction.objectStore('subject_settings')
    const request = objectStore.put(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('更新成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('更新失败', event.target.error)
    }
  }

  deleteSubjectSettingsItem(id) {
    const transaction = this.db.transaction(['subject_settings'], 'readwrite')
    const objectStore = transaction.objectStore('subject_settings')
    const request = objectStore.delete(id)

    request.onsuccess = (event) => {
      if (event.type == 'success') {
        // 成功
      }
    }
  }
}

class Contact {
  constructor() {
    this.dbName = 'snd_ai_contact'
    this.dbVersion = 1
    this.db = undefined // 数据库对象
  }

  init(studentId) {
    return new Promise((resolve) => {
      // 打开一个数据库并获取其reques对象
      const request = indexedDB.open(`${this.dbName}_user_${studentId}`, this.dbVersion)

      // 数据库打开失败
      request.onerror = (event) => {
        console.error('indexDB: 打开数据库失败！', event.target.error)
        resolve({
          success: false,
          data: null
        })
      }

      // 数据库打开成功
      request.onsuccess = (event) => {
        const db = event.target.result
        // console.log('indexDB: 打开数据库成功', db)
        this.db = db
        resolve({
          success: true,
          data: null
        })
      }

      // 数据库版本变更时候会触发
      request.onupgradeneeded = (event) => {
        // 数据库对象
        const db = event.target.result
        console.warn('indexDB: 数据库版本变更', db.version)
        // 创建一个数据表
        db.createObjectStore('private_message_history', {
          keyPath: 'parent_im_id'
        })
        db.createObjectStore('group_message_history', {
          keyPath: 'parent_im_id'
        })
        this.db = db
      }
    })
  }

  setMessageHistoryItem(data) {
    const transaction = this.db.transaction(['private_message_history'], 'readwrite')
    const objectStore = transaction.objectStore('private_message_history')
    const request = objectStore.add(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('添加成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('添加失败', event.target.error)
    }
  }

  getMessageHistoryItem(parentImId) {
    return new Promise((resolve) => {
      const transaction = this.db.transaction(['private_message_history'], 'readwrite')
      const objectStore = transaction.objectStore('private_message_history')
      const request = objectStore.get(parentImId)
      // 成功
      request.onsuccess = (event) => {
        if (event.target.result) {
          const { messages } = event.target.result
          resolve({
            success: true,
            data: messages
          })
        } else {
          resolve({
            success: false,
            data: null
          })
        }
      }
    })
  }

  putMessageHistoryItem(data) {
    const transaction = this.db.transaction(['private_message_history'], 'readwrite')
    const objectStore = transaction.objectStore('private_message_history')
    const request = objectStore.put(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('更新成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('更新失败', event.target.error)
    }
  }

  deleteMessageHistoryItem(parentImId) {
    const transaction = this.db.transaction(['private_message_history'], 'readwrite')
    const objectStore = transaction.objectStore('private_message_history')
    const request = objectStore.delete(parentImId)

    request.onsuccess = (event) => {
      if (event.type == 'success') {
        // 成功
      }
    }
  }
}

class DailyAttendance {
  constructor() {
    this.dbName = 'snd_ai_daily_attendance'
    this.dbVersion = 8
    this.db = undefined // 数据库对象
  }

  init(studentId) {
    // 打开一个数据库并获取其reques对象
    const request = indexedDB.open(`${this.dbName}_user_${studentId}`, this.dbVersion)

    // 数据库打开失败
    request.onerror = (event) => {
      console.error('indexDB: 打开数据库失败！', event.target.error)
    }

    // 数据库打开成功
    request.onsuccess = (event) => {
      const db = event.target.result
      // console.log('indexDB: 打开数据库成功', db)
      this.db = db
    }

    // 数据库版本变更时候会触发
    request.onupgradeneeded = (event) => {
      // 数据库对象
      const db = event.target.result
      console.warn('indexDB: 数据库版本变更', db.version)

      if (db.objectStoreNames.contains('cache')) {
        db.deleteObjectStore('cache')
      }

      // 创建一个数据表
      db.createObjectStore('cache', {
        keyPath: 'id'
      })

      if (!db.objectStoreNames.contains('task_ids')) {
        // 创建一个数据表
        db.createObjectStore('task_ids', {
          keyPath: 'subjectId'
        })
      }

      this.db = db
    }
  }

  setCacheItem(data) {
    const transaction = this.db.transaction(['cache'], 'readwrite')
    const objectStore = transaction.objectStore('cache')
    const request = objectStore.add(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('添加成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('添加失败', event.target.error)
    }
  }

  getCacheItem(id) {
    return new Promise((resolve) => {
      const transaction = this.db.transaction(['cache'], 'readwrite')
      const objectStore = transaction.objectStore('cache')
      const request = objectStore.get(id)
      // 成功
      request.onsuccess = (event) => {
        if (event.target.result) {
          const { value } = event.target.result
          resolve({
            success: true,
            data: value
          })
        } else {
          resolve({
            success: false,
            data: null
          })
        }
      }
    })
  }

  putCacheItem(data) {
    const transaction = this.db.transaction(['cache'], 'readwrite')
    const objectStore = transaction.objectStore('cache')
    const request = objectStore.put(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('更新成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('更新失败', event.target.error)
    }
  }

  deleteCacheItem(id) {
    const transaction = this.db.transaction(['cache'], 'readwrite')
    const objectStore = transaction.objectStore('cache')
    const request = objectStore.delete(id)

    request.onsuccess = (event) => {
      if (event.type == 'success') {
        // 成功
      }
    }
  }

  setTaskIdsItem(data) {
    const transaction = this.db.transaction(['task_ids'], 'readwrite')
    const objectStore = transaction.objectStore('task_ids')
    const request = objectStore.add(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('添加成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('添加失败', event.target.error)
    }
  }

  getTaskIdsItem(id) {
    return new Promise((resolve) => {
      const transaction = this.db.transaction(['task_ids'], 'readwrite')
      const objectStore = transaction.objectStore('task_ids')
      const request = objectStore.get(id)
      // 成功
      request.onsuccess = (event) => {
        if (event.target.result) {
          const { value } = event.target.result
          resolve({
            success: true,
            data: value
          })
        } else {
          resolve({
            success: false,
            data: null
          })
        }
      }
    })
  }

  putTaskIdsItem(data) {
    const transaction = this.db.transaction(['task_ids'], 'readwrite')
    const objectStore = transaction.objectStore('task_ids')
    const request = objectStore.put(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('更新成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('更新失败', event.target.error)
    }
  }

  deleteTaskIdsItem(id) {
    const transaction = this.db.transaction(['task_ids'], 'readwrite')
    const objectStore = transaction.objectStore('task_ids')
    const request = objectStore.delete(id)

    request.onsuccess = (event) => {
      if (event.type == 'success') {
        // 成功
      }
    }
  }
}

class ClassGuidance {
  constructor() {
    this.dbName = 'snd_ai_class_guidance'
    this.dbVersion = 8
    this.db = undefined // 数据库对象
  }

  init(studentId) {
    // 打开一个数据库并获取其reques对象
    const request = indexedDB.open(`${this.dbName}_user_${studentId}`, this.dbVersion)

    // 数据库打开失败
    request.onerror = (event) => {
      console.error('indexDB: 打开数据库失败！', event.target.error)
    }

    // 数据库打开成功
    request.onsuccess = (event) => {
      const db = event.target.result
      // console.log('indexDB: 打开数据库成功', db)
      this.db = db
    }

    // 数据库版本变更时候会触发
    request.onupgradeneeded = (event) => {
      // 数据库对象
      const db = event.target.result
      console.warn('indexDB: 数据库版本变更', db.version)

      if (!db.objectStoreNames.contains('guidance_ids')) {
        // 创建一个数据表
        db.createObjectStore('guidance_ids', {
          keyPath: 'subjectId'
        })
      }

      this.db = db
    }
  }

  setGuidanceIdsItem(data) {
    const transaction = this.db.transaction(['guidance_ids'], 'readwrite')
    const objectStore = transaction.objectStore('guidance_ids')
    const request = objectStore.add(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('添加成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('添加失败', event.target.error)
    }
  }

  getGuidanceIdsItem(id) {
    return new Promise((resolve) => {
      const transaction = this.db.transaction(['guidance_ids'], 'readwrite')
      const objectStore = transaction.objectStore('guidance_ids')
      const request = objectStore.get(id)
      // 成功
      request.onsuccess = (event) => {
        if (event.target.result) {
          const { value } = event.target.result
          resolve({
            success: true,
            data: value
          })
        } else {
          resolve({
            success: false,
            data: null
          })
        }
      }
    })
  }

  putGuidanceIdsItem(data) {
    const transaction = this.db.transaction(['guidance_ids'], 'readwrite')
    const objectStore = transaction.objectStore('guidance_ids')
    const request = objectStore.put(data)
    // 成功
    request.onsuccess = (event) => {
      // console.log('更新成功')
    }
    // 失败
    request.onerror = (event) => {
      console.error('更新失败', event.target.error)
    }
  }

  deleteGuidanceIdsItem(id) {
    const transaction = this.db.transaction(['guidance_ids'], 'readwrite')
    const objectStore = transaction.objectStore('guidance_ids')
    const request = objectStore.delete(id)

    request.onsuccess = (event) => {
      if (event.type == 'success') {
        // 成功
      }
    }
  }
}

export default {
  // 作业&考试
  homework: new Homework(),
  // 作业&考试成绩录入
  homework_input: new HomeworkInput(),
  // 联系家长
  contact: new Contact(),
  // 每日打卡
  daily_attendance: new DailyAttendance(),
  // 课堂导学
  class_guidance: new ClassGuidance()
}
